<?php

class Model_Skills extends Zend_Db_Table_Abstract
{
    public function getIndicators($idcargo){
  
        $sql = sprintf("SELECT c.IDCOMPETENCIA, c.COMPETENCIA, cc.PESO, IFNULL(i.IDINDICADOR, 0) AS IDINDICADOR, IFNULL(i.INDICADOR, '') AS INDICADOR
                        FROM SP_COMPETENCIA c, SP_CARGO_COMPETENCIA cc
                        LEFT JOIN SP_INDICADOR i ON i.IDCOMPETENCIA = cc.IDCOMPETENCIA
                        WHERE cc.IDCOMPETENCIA = c.IDCOMPETENCIA AND cc.IDCARGO = %d
                        ORDER BY c.TIPO DESC, c.COMPETENCIA, i.INDICADOR", $idcargo);
        
        $db = $this->getDefaultAdapter();
        $stmt = $db->query($sql);
        $data = $stmt->fetchAll();
              
        return $data;
    }
    
    public function saveResults($skills, $numid){
       
        $response = 1;
     try{
        $arr = array();
        for($i = 0; $i < count($skills); $i++){
            list($field, $val) = explode('=', $skills[$i]);
            list($prefix, $idcompetencia, $idindicador) = explode('_', $field);
            $arr[$idcompetencia][$idindicador] = $val;
        }
                       
        foreach ($arr as $idcompetencia => $vArr1) {
           foreach ($vArr1 as $idindicador => $calc) {
               $vals[] = sprintf("(%d,%d,%d,%f)",
                       $numid,
                       $idcompetencia,
                       $idindicador,
                       $calc);   
           }          
        }
        $sql = "INSERT INTO PMT_SP_CALIFICACION_COMPETENCIAS
           (NUM_IDENTIFICACION,IDCOMPETENCIA,IDINDICADOR,CALIFICACION) VALUES".implode(',', $vals);
        
        
        $sql2 = "DELETE FROM PMT_SP_CALIFICACION_COMPETENCIAS WHERE NUM_IDENTIFICACION = ".$numid;
        
        $db = $this->getDefaultAdapter();
        $stmt = $db->exec($sql2);
        $stmt = $db->exec($sql);
        //$data = $stmt->fetchAll();
        
        
     }catch (Exception $e){
         
        $response = $e->getMessage(); 
         
     }
     return $response;
    }
}

